//
//  MainViewController.swift
//  02- 新浪微博(swift实现)
//
//  Created by 刘 伟生 on 16/1/7.
//  Copyright © 2016年 seasa. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    //创建一个数组,保存title和imagename
    
    lazy  var titles : [String] = {
        return ["首页", "消息", "", "发现", "我"]
    }()
    
    lazy var imageNames : [String] = {
        return ["tabbar_home", "tabbar_message_center", "", "tabbar_discover", "tabbar_profile"]
        }()
    
    lazy var conposeBtn : UIButton  = {
        let btn = UIButton.createComposeBtn("tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")
        // 2.设置位置
        btn.center = CGPoint(x: self.tabBar.center.x, y: self.tabBar.bounds.size.height * 0.5)
        
        // 3.监听按钮的点击
        // Selector两种写法
        // 第一种写法:Selector("方法的名称")
        // 第二种写法:"方法的名称"
        btn.addTarget(self, action: "composeBtnClick", forControlEvents: .TouchUpInside)
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 添加子控制器
        /*
        addAllChildrenControll("HomeVcontroller", title: "首页", imageName: "tabbar_home")
        
        addAllChildrenControll("MessegeController", title: "消息", imageName: "tabbar_message_center")
        addAllChildrenControll("DiscoverController", title: "发现", imageName: "tabbar_discover")
        addAllChildrenControll("ProfileController", title: "我", imageName: "tabbar_profile")
        */
        
        tabBar .addSubview(conposeBtn)
        
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //给所有的子控制器添加名称和图片
        setUpTabBarItem()
        
    }
    
    func setUpTabBarItem() {
        
        //循环遍历数组
        for i in 0..<tabBar.items!.count {
            
            let item = tabBar.items![i]
            if i == 2 {
                item.enabled = false
                continue
            }
            item.title = titles[i]
            item.image = UIImage(named: imageNames[i])
            item.selectedImage = UIImage(named: imageNames[i] + "_highlighted")
            
        }
    }
    
    
    func addAllChildrenControll(controllerName : String, title : String, imageName : String){
        
        //创建一个navigationViewController
        let navigationVC = UINavigationController()
        
        //根据闯入来的数据类型创建一个控制器,作为navigationViewController的跟控制器
        //首先要获得控制器的类型真实,但是值得注意的是,外界所写的数据类型不是真是的数据类型,因为他还需要加上他的命名空间才是他的数据类型
        
        //动态获取命名空间
        
        let vc = HomeVcontroller()
        print(vc)
        
        guard let nameSpace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as? String else {
            print("获取命名空间失败")
            return
        }


        //通过字符串找到对应的class
        guard let AnyClass = NSClassFromString("\(nameSpace)." + controllerName) else {
            print("没有找到对应的class")
            return
        }
        
        //根据class找到真实的数据类型
        guard let controller = AnyClass as? UITableViewController.Type else {
            print("没有找到真实的控制器类型")
            return
        }
        
        let homeV = controller.init()
        
        //将tableViewController设置为navigationViewController的跟控制器
        navigationVC .addChildViewController(homeV)
        
        //设置标题和tabItem的名称
        homeV.navigationItem.title = title
        
        navigationVC.tabBarItem.title = title
        navigationVC.tabBarItem.image = UIImage(named: imageName)
        navigationVC.tabBarItem.selectedImage = UIImage(named: imageName + "heightlight")
        
        
        //添加为子控制器
        addChildViewController(navigationVC)
        
    }
    
    //监听方法
    func composeBtnClick() {
        print("你好帅")
    }

  
}
